<?php

class Contact extends Model {

    private $table = "contacts";
    private $id = 0;
    private $user = "";
    private $name = "";
    private $email = "";
    private $fone = "";

    public function getId() {
        return $this->id;
    }

    public function getUser() {
        return $this->user;
    }

    public function getName() {
        return $this->name;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getFone() {
        return $this->fone;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function setFone($fone) {
        $this->fone = $fone;
    }

    public function insert() {
        $query = "insert into $this->table(user,name,email,fone) "
                . "values('$this->user','$this->name','$this->email','$this->fone')";
        if (mysql_query($query) >= 1) {
            return true;
        } else {
            return false;
        }
    }

    public function update() {
        $query = "update $this->table set user = '$this->user',"
                . "name = '$this->name',email = '$this->email',fone = '$this->fone' where id = $this->id";
        if (mysql_query($query) >= 1) {
            $user = new User();
            return true;
        } else {
            return false;
        }
    }

    public function delete() {
        $query = "delete from $this->table where id = $this->id";
        if (mysql_query($query) >= 1) {
            return true;
        } else {
            return false;
        }
    }

    public function selectByUser($user) {
        $query = "select * from $this->table where user = $user";
        $result = mysql_query($query);
        return $this->prepareArray($result);
    }

    public function selectByName($user,$name) {
        $query = "select distinct * from $this->table where user = $user and id in (select id from $this->table where name  like '%$name%' or email like '%$name%' or fone like '%$name%')";
        $result = mysql_query($query);
        return $this->prepareArray($result);
    }
    public function selectById($id) {
        $query = "select * from $this->table where id = $id";
        $result = mysql_query($query);
        $o = mysql_fetch_object($result);
        $this->setId($id);
        $this->setUser($o->user);
        $this->setName($o->name);
        $this->setEmail($o->email);
        $this->setFone($o->fone);
    }

    private function prepareArray($result) {
        $contacts = null;
        while ($o = mysql_fetch_object($result)) {
            $c = new Contact();
            $c->setId($o->id);
            $c->setUser($o->user);
            $c->setName($o->name);
            $c->setEmail($o->email);
            $c->setFone($o->fone);
            $contacts[] = $c;
        }
        return $contacts;
    }

}

?>
